(function() {
	var $ = require(L('$')), _ = require(L('_'));

	var win = $.createWindow(L('win_home'), true);
	var ui = $.layout('home');
	ui.view.add($.icon_info());
	win.nav.window.add(ui.view);
	$.tap(ui.btn_signin, function() {
		$.win('login').reset();
		$.win('home').nav.push($.win('login'));
	});
	$.tap(ui.btn_signup, function() {
		$.win('register').reset();
		$.win('home').nav.push($.win('register'));
	});
	$.tap(ui.btn_signin_fb, function() {
		$.mask();
		$.facebook().logout();
		$.facebook().login(function(e) {
			if (e && e.success && e.data && e.data.id) {
				$.webservice().checkExistedFBAccount(e.data.id, function(e2) {
					var result = JSON.parse(e2);
					if (!result.IsExisted) {
						$.ui('popup').terms_and_conditions().display(function() {
							helper.login_fb(e.data);
						});
					} else {
						helper.login_fb(e.data);
					}
				});
			} else {
				$.mask(false);
			}
		});
	});
	var helper = {};
	helper.login_fb = function(data) {
		$.webservice().loginByFB(data.id, data.first_name, data.last_name, function(e) {
			var result = JSON.parse(e);
			if (result.Success === false) {
				$.a(result.Error);
			} else {
				$.currentUserID(result.UserID);
				var email = result.EmailAddress || '';
				$.currentUserEmailAddress(email);
				//$.currentUserEmailAddress(result.EmailAddress);
				$.currentUserName(result.UserName);
				$.allowReceiveNotification(result.AllowReceiveRetailerNotification);
				$.badgeNotification(result.UnreadNotificationNumber);
				$.badgeShoppingBag(result.CheckoutNotificationNumber);
				$.openTabgroupAndFocusToMyDesires();
			}
		});
	};
	module.exports = win;
})();
